//SPDX-License-Identifier:	GPL-2.0+

#ifndef __LS2K500_COMMON_H
#define __LS2K500_COMMON_H

#include <linux/sizes.h>

/* Loongson bootelf bootparam smbios */
#define CONFIG_LOONGSON_BOOT_FIXUP
#define CONFIG_SMBIOS_PRODUCT_NAME "Loongson 2K500"
#define CONFIG_SMBIOS_MANUFACTURER "Loongson"

/* Loongson LS2K500 clock configuration. */
#define OSC_CLK         100000000 //LS2K使用内部参考时钟100MHz（固定）

#define CPU_CLOCK_RATE  600000000	/* 定义cpu核心频率为 500~750MHz */
#define DDR_CLOCK_RATE  400000000	/* 定义ddr工作频率为 400~600Mhz */
#define APB_CLOCK_RATE  125000000	/* 定义apb频率为最大值为 MHz for BOOT, SATA, USB, APB */
#define GMAC_CLOCK_RATE 125000000	/* 定义gmac工作频率为125 MHz 必须固定125MHz */
#define GPU_CLOCK_RATE  200000000
#define NETWORK_CLOCK_RATE  400000000	/* 注意：NETWORK输入频率来源是DDR, 所以NETWORK频率不要超过DDR频率 */

#define CPU_TCLOCK_RATE CPU_CLOCK_RATE
#define CONFIG_SYS_MIPS_TIMER_FREQ  (CPU_TCLOCK_RATE / 2)
#define CONFIG_SYS_HZ   1000

/* memory */
#ifdef CONFIG_32BIT
#define CONFIG_SYS_SDRAM_BASE		0x80000000	/* Cached addr */
#define CONFIG_SYS_SDRAM_SIZE		SZ_1G
#define CONFIG_SYS_INIT_SP_OFFSET	0x00400000
#define CONFIG_SYS_LOAD_ADDR		0x83000000
#define FDT_LOAD_ADDR		0x8b000000
#define RAMDISK_START		0x88000000
#define RAMDISK_SIZE		0x02000000
#endif
#ifdef CONFIG_64BIT
#ifdef CONFIG_LOONGARCH
#define CONFIG_SYS_SDRAM_BASE		0x9000000080000000 /* cached address */
#define CONFIG_SYS_SDRAM_SIZE		SZ_1G
#define CONFIG_SYS_INIT_SP_OFFSET	0x00400000
#define CONFIG_SYS_INIT_SP_ADDR		(CONFIG_SYS_TEXT_BASE + CONFIG_SYS_INIT_SP_OFFSET)
#define CONFIG_SYS_LOAD_ADDR		0x9000000083000000
#define FDT_LOAD_ADDR		0x900000008a000000
#define RAMDISK_START		0x9000000087000000
#define RAMDISK_SIZE		0x02000000
#endif
#endif

#ifdef CONFIG_MIPS
#ifdef CONFIG_SPL
#define CONFIG_SPL_MAX_SIZE	SZ_256K
#define CONFIG_SPL_BSS_START_ADDR	0x98020000
#define CONFIG_SPL_BSS_MAX_SIZE		0x00004000
#define BOOT_SPACE_BASE		    0x98000000
#define LOONGSON_SP_ADDR	(CONFIG_SPL_TEXT_BASE + CONFIG_SYS_INIT_SP_OFFSET)
#else
#define LOONGSON_SP_ADDR	(CONFIG_SYS_TEXT_BASE + CONFIG_SYS_INIT_SP_OFFSET)
#endif //CONFIG_SPL
#endif //CONFIG_MIPS

/* DMA */
//#define CONFIG_DMA_COHERENT
//#define CONFIG_DMA_COHERENT_SIZE	(1 << 20)

/* Miscellaneous configurable options */
#define CONFIG_SYS_MAXARGS 64	/* max number of command args */
#define CONFIG_SYS_CBSIZE 512 /* Console I/O Buffer Size */

#define CONFIG_SYS_MONITOR_BASE	CONFIG_SYS_TEXT_BASE
//#define CONFIG_SYS_MONITOR_LEN		(192 << 10)
#define CONFIG_SYS_MALLOC_LEN	(28 * 1024 * 1024)

/* 串口打印信息，汇编调试 */
#define CONFIG_PRINTK
#ifndef CONFIG_DM_SERIAL
#define CONFIG_CPU_UART
#define CONFIG_CONS_INDEX	1
#define CONFIG_SYS_NS16550_SERIAL
#define CONFIG_SYS_NS16550_REG_SIZE	 1
#endif
#define CONFIG_SYS_NS16550_CLK	APB_CLOCK_RATE
#ifdef CONFIG_MIPS
#define CONFIG_SYS_NS16550_COM1	 CKSEG1ADDR(0x1ff40800)
#endif
#ifdef CONFIG_LOONGARCH
#define CONFIG_SYS_NS16550_COM1	 PHYS_TO_UNCACHED(0x1ff40800)
#endif
#define UART_BASE_ADDR	CONFIG_SYS_NS16550_COM1

/* LS1X SPI Settings */
#ifdef CONFIG_LS1X_SPI
//#define CONFIG_LS1X_SPI1_ENABLE	/* 使用spi1控制器(复用设置), spi1控制器复用can，如果不使用spi1，最好把该选项关闭 */
#define CONFIG_SPI_CS
//#define CONFIG_SPI_CS_USED_GPIO
#endif

/* Environment settings */
#ifdef CONFIG_ENV_IS_IN_SPI_FLASH
#define CONFIG_ENV_SIZE			0x4000	/* 16KB */
#define CONFIG_ENV_SPI_CS		0
#define CONFIG_ENV_SPI_MAX_HZ	30000000
#define CONFIG_ENV_OFFSET		0xec000	/* 根据分区大小修改，这里是SPI Flash uboot_env的偏移地址 */
#define CONFIG_ENV_SECT_SIZE	(4 << 10)
/*
 * Environment is right behind U-Boot in flash. Make sure U-Boot
 * doesn't grow into the environment area.
 */
#define CONFIG_BOARD_SIZE_LIMIT		CONFIG_ENV_OFFSET
#elif CONFIG_ENV_IS_IN_EEPROM
#define CONFIG_ENV_SIZE			0x800	/* 2KB */
#define CONFIG_ENV_OFFSET		0x0
#define CONFIG_ENV_EEPROM_IS_ON_I2C
#define CONFIG_SYS_DEF_EEPROM_ADDR	0x50 /* i2a slave addr */
#define CONFIG_BOARD_SIZE_LIMIT		0xec000
#else
#define CONFIG_ENV_SIZE			0x800	/* 2KB */
#define CONFIG_ENV_OFFSET		0x0
#define CONFIG_BOARD_SIZE_LIMIT		0xec000
#endif

/* NAND settings */
#ifdef CONFIG_NAND
#define CONFIG_SYS_NAND_ONFI_DETECTION
#define CONFIG_SYS_NAND_SELF_INIT
#define CONFIG_SYS_MAX_NAND_DEVICE	1
#define CONFIG_SYS_NAND_BASE	LS2X_NAND_BASE
#define NAND_LS1X_CS 0
#define NAND_LS1X_MAX_CHIPS 1
#define NAND_LS2K_DMACHAN 0
#define CONFIG_NAND_ECC_BCH
#endif

/* PCI */
#ifdef  CONFIG_PCIE_LOONGSON2_SOC
//#define CONFIG_PCI_CONFIG_HOST_BRIDGE
#define CONFIG_PCI_SCAN_SHOW
#endif

/* SCSI */
#define LOONGSON_SATA_USED_INTERNRL_CLK
#if defined(CONFIG_SCSI_AHCI) && !defined(CONFIG_DM_SCSI)
#define SCSI_VEND_ID 0x0014
#define SCSI_DEV_ID  0x7a08
#define CONFIG_SCSI_DEV_LIST {SCSI_VEND_ID, SCSI_DEV_ID}
#if !defined(CONFIG_PCI)
#define CONFIG_SCSI_AHCI_PLAT
#endif
#define CONFIG_SYS_SCSI_MAX_SCSI_ID	1
#define CONFIG_SYS_SCSI_MAX_LUN	1
#define CONFIG_SYS_SCSI_MAX_DEVICE	(CONFIG_SYS_SCSI_MAX_SCSI_ID * CONFIG_SYS_SCSI_MAX_LUN)
#endif

/* USB */
#ifdef CONFIG_USB_OHCI_HCD
#define CONFIG_USB_OHCI_NEW
#define CONFIG_SYS_USB_OHCI_MAX_ROOT_PORTS	8
#endif

/* Ethernet driver configuration */
#if defined(CONFIG_LOONGSON_GMAC) || defined(CONFIG_ETH_DESIGNWARE)
#define ENH_DESC //ls2x的gmac控制器需要把该宏定义打开
#define CONFIG_DW_GMAC_DEFAULT_DMA_PBL 4
#define CONFIG_DW_ALTDESCRIPTOR
#define PHY_ANEG_TIMEOUT	8000
#endif
#define CONFIG_NET_MULTI
#define CONFIG_NET_RETRY_COUNT          5

#define CONFIG_ETHADDR		08:00:3e:26:0a:5b
#define CONFIG_IPADDR		192.168.1.20
#define CONFIG_NETMASK		255.255.255.0
#define CONFIG_SERVERIP		192.168.1.2

#if defined(CONFIG_VIDEO) || defined(CONFIG_DM_VIDEO)
#define VIDEO_FB_16BPP_WORD_SWAP
#define CONFIG_SPLASH_SCREEN
#define CONFIG_SPLASH_SCREEN_ALIGN
/* logo 设置 */
#define CONFIG_VIDEO_LOGO
#define CONFIG_VIDEO_BMP_RLE8
#define CONFIG_VIDEO_BMP_LOGO
#define CONFIG_BMP_16BPP
#define CONFIG_BMP_24BPP
#define CONFIG_BMP_32BPP
#endif

#ifdef CONFIG_VIDEO
#define CONSOLE_STDOUT_SETTINGS \
	"stdin=serial,usbkbd\0" \
	"stdout=serial\0" \
	"stderr=serial,vga\0"
#elif CONFIG_DM_VIDEO
#define CONSOLE_STDOUT_SETTINGS \
	"splashimage=" __stringify(CONFIG_SYS_LOAD_ADDR) "\0" \
	"stdin=serial,usbkbd\0" \
	"stdout=serial\0" \
	"stderr=serial,vidconsole,vidconsole1\0"
#else
#define CONSOLE_STDOUT_SETTINGS \
	"stdin=serial\0" \
	"stdout=serial\0" \
	"stderr=serial\0"
#endif

#ifndef CONFIG_DM_I2C
#define CONFIG_SYS_I2C
#define CONFIG_SYS_LOONGSON_I2C0_SPEED	400000
#define CONFIG_SYS_LOONGSON_I2C1_SPEED	400000
#define CONFIG_SYS_LOONGSON_I2C0_SLAVE	0x39
#define CONFIG_SYS_LOONGSON_I2C1_SLAVE	0x39
#endif
#ifdef CONFIG_SYS_I2C_LOONGSON
#define CONFIG_SYS_I2C_LOONGSON_CH0	CKSEG1ADDR(0x1fe01000)
#define CONFIG_SYS_I2C_LOONGSON_CH1	CKSEG1ADDR(0x1fe01800)
#endif

#ifdef CONFIG_RECOVER
#define RECOVER_DEFAULT_ENV setenv bootargs console=ttyS0,115200 rd_start=${rd_start} rd_size=${rd_size} \
mtdparts=${mtdparts} \
root=/dev/ram rw rootfstype=ext2 \
video=${video};\
bootm ${loadaddr}

#define RECOVER_USB_DEFAULT "usb start;fatload usb 0:1 ${loadaddr} /install/uImage;fatload usb 0:1 ${rd_start} /install/ramdisk.gz;\
" __stringify(RECOVER_DEFAULT_ENV) ""

#define RECOVER_MMC_DEFAULT "mmc rescan;fatload mmc 0:1 ${loadaddr} /install/uImage;fatload mmc 0:1 ${rd_start} /install/ramdisk.gz;\
" __stringify(RECOVER_DEFAULT_ENV) ""

#define BOOT_SATA_DEFAULT "setenv bootargs console=ttyS2,115200 \
rootfstype=ext4 rw rootwait; \
setenv bootcmd ' setenv bootargs ${bootargs} root=/dev/${root_dev}${syspart} mtdparts=${mtdparts} video=${video}; \
sf probe;sf read ${fdt_addr} dtb;scsi scan;ext4load scsi 0:${syspart} ${loadaddr} /boot/uImage;bootm ';\
saveenv;\
boot"
#endif //CONFIG_RECOVER

#define BOOT_SATA_DEFAULT "setenv bootargs console=ttyS2,115200 \
rootfstype=ext4 rw rootwait; \
setenv bootcmd ' setenv bootargs ${bootargs} root=/dev/${root_dev}${syspart} mtdparts=${mtdparts} video=${video}; \
sf probe;sf read ${fdt_addr} dtb;scsi scan;ext4load scsi 0:${syspart} ${loadaddr} /boot/uImage;bootm ';\
saveenv;\
boot"

#define BOOT_NAND_DEFAULT "setenv bootargs console=ttyS2,115200 \
root=ubi0:rootfs ubi.mtd=root,2048 noinitrd init=/linuxrc rootfstype=ubifs rw; \
setenv bootcmd ' setenv bootargs ${bootargs} mtdparts=${mtdparts} video=${video}; \
sf probe;sf read ${fdt_addr} dtb;nboot kernel;bootm ';\
saveenv;\
boot"

#define CONFIG_SYS_BOOTM_LEN	SZ_64M

#endif	/* __LS2K500_COMMON_H */